package entity;

//成绩类
public class Score {
    private String studentId;
    private double chinese;
    private double math;
    private double english;
    private final double totalScore;
    
    public Score(String studentId, double chinese, double math, double english) {
        this.studentId = studentId;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
        this.totalScore = chinese + math + english;
    }

    public String getStudentId() {
        return studentId;
    }

    public double getChinese() {
        return chinese;
    }

    public double getMath() {
        return math;
    }

    public double getEnglish() {
        return english;
    }

    public double getTotalScore() {
        return totalScore;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    public void setChinese(double chinese) {
        this.chinese = chinese;
        updateTotalScore();
    }

    public void setMath(double math) {
        this.math = math;
        updateTotalScore();
    }

    public void setEnglish(double english) {
        this.english = english;
        updateTotalScore();
    }
    
    /**
     * 更新总分
     */
    private void updateTotalScore() {
        try {
            // 使用反射更新final字段
            java.lang.reflect.Field field = Score.class.getDeclaredField("totalScore");
            field.setAccessible(true);
            java.lang.reflect.Field modifiersField = java.lang.reflect.Field.class.getDeclaredField("modifiers");
            modifiersField.setAccessible(true);
            modifiersField.setInt(field, field.getModifiers() & ~java.lang.reflect.Modifier.FINAL);
            field.set(this, chinese + math + english);
        } catch (Exception e) {
            // 如果无法更新final字段，可以忽略
        }
    }
    
    /**
     * 获取平均分
     */
    public double getAverageScore() {
        return totalScore / 3.0;
    }
    
    /**
     * 获取等级评定
     */
    public String getGradeLevel() {
        double average = getAverageScore();
        if (average >= 90) return "优秀";
        if (average >= 80) return "良好";
        if (average >= 70) return "中等";
        if (average >= 60) return "及格";
        return "不及格";
    }

    @Override
    public String toString() {
        return "{" +
                "studentId='" + studentId + '\'' +
                ", chinese=" + chinese +
                ", math=" + math +
                ", english=" + english +
                ", totalScore=" + totalScore +
                ", averageScore=" + String.format("%.2f", getAverageScore()) +
                ", gradeLevel='" + getGradeLevel() + '\'' +
                '}';
    }
}